#include <bits/stdc++.h>
using namespace std;
using uint=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128;
const int N=2e5+5;

int n,a[N],t[N];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int l=1,r=1,ans=0;
    while(l<=n&&r<=n){
        while(r<l)t[a[r]]++,r++;
        while(r<=n&&(a[r]==a[r+1]&&t[a[r]]==0)||(a[r]==a[r-1]&&t[a[r]]==1)){
            t[a[r]]++;
            r++;
        }
        ans=max(ans,r-l);
        t[a[l]]--;
        l++;
    }
    cout<<ans<<endl;

    return 0;
}